package  
{
	/**
   * ...
   * @author ivan
   */
  public class CrappyOldCode 
  {
    
    public function CrappyOldCode() 
    {
      
      /*
      var line:Line = new Line(new Point(0, 0), new Point(1, 1));
      var line2:Line = new Line(new Point(0, 1), new Point(1, 0));
      trace(line);
      trace(line.slope);
      trace(line.intersectLine(line2));
      trace(line2.intersectLine(line));*/
    }
    
    // Add some trees
    // TODO: Add them to scene as static geometry.
    private function addTrees():void
    {
      var bmpTree:Bitmap = new rTree;
      var treeMaterial:BitmapMaterial = new BitmapMaterial(bmpTree.bitmapData);
      // treeMaterial.transparent = true;
      for (var i:int = 0; i < 300; i++)
      {
        var sprite3D:Sprite3D = new Sprite3D(treeMaterial, 100, 200);       
        var x:Number = Math.random() * 15000 - 15000/2;
        var z:Number = Math.random() * 15000 - 15000/2;
        sprite3D.x = x;
        sprite3D.z = z;
        sprite3D.y = _heightMap.heightAtPosition(x, z) + 100;
        _view.scene.addChild(sprite3D);
      }
    }
    
    private function buildShadowMap(bmp:BitmapData, sunDirection:Vector3D):BitmapData
    {
        if ( sunDirection.equals(new Vector3D(0, -1, 0)) )
            return null;
        
        var sm:BitmapData = new BitmapData(bmp.width, bmp.height, false);
        
        for (var i:int = 0; i < bmp.width; i++)
        for (var j:int = 0; j < bmp.height; j++)
        {
            // Sun projected in floor position (limited to map border).
            var sunPos2D:Point = getLimitCollision( new Rectangle(0, 0, bmp.width, bmp.height), 
                                                    new Point(i, j),
                                                    new Point( -sunDirection.x, -sunDirection.z) );
            
            var line:Line = new Line(new Point(i, j), new Point(sunPos2D.x, sunPos2D.y));
            
            var points:Vector.<Point> = line.pixelate();
            
            var currentTestedPointHeight:Number = _heightMap.heightAtMapPosition(i, j);
            
            for (var k:int = 1; k < points.length; k++)
            {
                var pointHeight:Number = _heightMap.heightAtMapPosition(points[k].x, points[k].y);
                // TODO Añadir comprobación aquí.
            }
        }
        return sm;
    }
        
    // Returns intersection from one side of r and the line given by startPoint and direction.
    private function getLimitCollision(r:Rectangle, s:Point, d:Point):Point
    {
        // TODO.
        var line:Line = new Line(s, s.add(d));
        
        var tr:Point = r.topLeft.add(new Point(r.width, 0));
        var bl:Point = r.bottomRight.subtract(new Point(r.width, 0));
        
        var up:Line = new Line(new Point(r.x, r.height), new Point(r.x + width, r.height));
        var right:Line = new Line(new Point(r.x, r.height), new Point(r.x + width, r.height));
        var down:Line = new Line(new Point(r.x, 0), new Point(r.x + width, 0));
        var left:Line = new Line(new Point(0, 0), new Point(0, r.height));
        
        var iu:Point = line.intersectLine(up);
        
        return null;
    }
    
  }

}